package com.laiat.yamba.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.laiat.yamba.Manifest;
import com.laiat.yamba.R;
import com.laiat.yamba.StatusData;
import com.laiat.yamba.TimelineReceiver;
import com.laiat.yamba.UpdaterService;

public class TimelineActivity extends BaseActivity {
	private static final String TAG = TimelineActivity.class.getSimpleName();
	
	private ListView listTimeline;
	
	private TimelineReceiver timelineReceiver;
	
	private static final int TTS_DATA_CHECK_CODE = 1;

	private Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline);
		
		timelineReceiver = new TimelineReceiver(this);
		
		//Find your view
		listTimeline = (ListView)findViewById(R.id.listTimeline);
		listTimeline.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		    	Log.d(TAG, "Item clicked");
		    	
		    	// When clicked, read the text
		    	TextToSpeech textToSpeech = yamba.getTextToSpeech();
				if (textToSpeech != null) {
					Log.d(TAG, "Speaking...");
		    		
		    		TextView textUser = (TextView)view.findViewById(R.id.textUser);
		    		String postedByStr = getString(R.string.postedBy) + " " + textUser.getText();
		    		textToSpeech.speak(postedByStr, TextToSpeech.QUEUE_FLUSH, null);
		    		
		    		TextView textTime = (TextView)view.findViewById(R.id.textCreatedAt);
		    		String timeStr = getString(R.string.postedOn) + " " + textTime.getText();
		    		textToSpeech.speak(timeStr, TextToSpeech.QUEUE_ADD, null);
		    		
		    		TextView textText = (TextView)view.findViewById(R.id.textText);
		    		String twitterText = textText.getText().toString();
		    		textToSpeech.speak(twitterText, TextToSpeech.QUEUE_ADD, null);
		    	}
		    }
		});
		
		
		if (!yamba.hasTwitterCredentials()) {
    		showDialog(DIALOG_TWITTER_LOGIN);
    	}
		
		checkTextToSpeechEngine();
	}

	private void checkTextToSpeechEngine() {
		//check textToSpeech engine
		if (yamba.getTextToSpeech(false) == null) {
			Intent checkIntent = new Intent();
			checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
			startActivityForResult(checkIntent, TTS_DATA_CHECK_CODE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 if (requestCode == TTS_DATA_CHECK_CODE) {
			 if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				 // success, create the TTS instance
				 yamba.setTextToSpeech(new TextToSpeech(yamba, yamba));
				 Log.d(TAG, "Created TextToSpeech");
			 } else {
				 // missing data, install it
				 Intent installIntent = new Intent();
				 installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				 startActivity(installIntent);
				 
				 Log.d(TAG, "Starting TextToSpeech installation.");
			 }
		 }
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		Log.d(TAG, "onResume");
		
		registerReceiver(timelineReceiver, 
						 new IntentFilter(UpdaterService.BROADCAST_UPDATE_TIMELINE_ACTION_NAME),
						 Manifest.permission.SEND_TIMELINE_NOTIFICATIONS,
						 null);
					
		refreshListView();

	}
	
	@Override
	protected void onPause() {

		//Unregister the receiver
		unregisterReceiver(timelineReceiver);

		super.onPause();
	}

	public void refreshListView() {
		Log.d(TAG, "refresh list");
		
		StatusData statusData = yamba.getStatusData();
		
		if (cursor != null) {
			stopManagingCursor(cursor);
			cursor.close();
		}
		cursor = statusData.getStatusUpdates();
		startManagingCursor(cursor);
		
		//Setup the adapter
		TimelineAdapter adapter = new TimelineAdapter(this, cursor);
		listTimeline.setAdapter(adapter);
	}
			
}
